package com.fyzk.activity.special;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.AppToast;
import com.fyzk.bean.VirtualCoinExchangeBean;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.utils.HelpUrlConstants;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import http.Common.CommandUtil;
import http.DataUtil.text.TextUtil;
import http.StringResultBean;

/**
 * Created by fanxs11 on 17/1/4.
 *
 * 虚拟币兑换界面
 */

@ContentView(R.layout.ac_virtualmoney_exchange)
public class VirtualMoneyExchangeActivity extends BaseActivity{

    @ViewInject(R.id.recharge_balance)
    private TextView recharge_balance;

    @ViewInject(R.id.recharge_rg_money)
    private RadioGroup recharge_rg_money;

    @ViewInject(R.id.recharge_rg_type)
    private RadioGroup recharge_rg_type;

    @ViewInject(R.id.recharge_edt_amount)
    private EditText recharge_edt_amount;

    @ViewInject(R.id.virtualmoney_exchange_ok)
    private TextView ok;

    private TopTitleView chz_top_rl;

    private long jiFen = 0;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        jiFen = getIntent().getLongExtra("jifen", 0);
        info();

    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }

    @Override
    public void onRightClick() {
        super.onRightClick();
        WebView webView = new WebView(this);
        webView.setPadding(20, 0, 20, 0);
        webView.loadUrl(HelpUrlConstants.RECHARGE_LIMIT);
        AlertDialog dialog = new AlertDialog.Builder(this).setTitle("限额说明").setView(webView)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).create();
        dialog.show();
    }

    /**
     */
    private void info() {

        recharge_rg_money.setOnCheckedChangeListener(amountChangedListener);
        recharge_rg_money.check(R.id.recharge_money_200);

        chz_top_rl = (TopTitleView) findViewById(R.id.chz_top_rl);
        chz_top_rl.setOnTitleClickListener(this);

        ok.setOnClickListener(this);


        recharge_balance.setText("可用积分:" + jiFen + "");

    }

    /**
     * 积分兑换虚拟钱
     */
    private void virtualCoinExchange(long virtualMoney) {
        JSONObject params = new JSONObject();
        try {
            params.put("payChannel", "virtualcoinpay");
            params.put("amount", virtualMoney);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        httpPostJson(CommandUtil.VIRTUALCOINEXCHANGE, Commands.VIRTUALCOINEXCHANGE, params, false);
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        switch (requestCode) {
            case CommandUtil.VIRTUALCOINEXCHANGE:
                VirtualCoinExchangeBean virtualCoinExchangeBean = JSON.parseObject(resultBean.getData(), VirtualCoinExchangeBean.class);
                AppToast.getToast().show(virtualCoinExchangeBean.getResult());
                finish();
                break;
        }

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);

        String money = recharge_edt_amount.getText().toString();
        if (TextUtil.isEmpty(money)) {
            recharge_edt_amount.setError("请输入虚拟币数量");
            return;
        }
        if (Integer.parseInt(money)<10) {
            AlertDialog limitAlert = new AlertDialog.Builder(this).setTitle("提示").setMessage("最低兑换数量为10币").setPositiveButton("我知道了", new DialogInterface.OnClickListener() {


                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            }).create();
            limitAlert.show();
            recharge_edt_amount.setText("10");
            return;
        }

        switch (v.getId()) {
            case R.id.virtualmoney_exchange_ok:
                long virtualMoney = 0;
                try {
                    virtualMoney = Long.parseLong(recharge_edt_amount.getText().toString());
                } catch (Exception e) {

                }

                if (jiFen/10 < virtualMoney) {
                    new AlertDialog.Builder(this).setTitle("提示").setMessage("您的积分不足!\n积分兑换虚拟币的比例为10:1").setPositiveButton("确定", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    }).create().show();
                } else {
                    virtualCoinExchange(virtualMoney);
                }
                break;

        }

    }


    @Override
    protected void onFailure(int requestCode, String errorCode, String message) {
        super.onFailure(requestCode, errorCode, message);

    }

    RadioGroup.OnCheckedChangeListener amountChangedListener = new RadioGroup
            .OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.recharge_money_50:
                    recharge_edt_amount.setText(50 + "");
                    break;
                case R.id.recharge_money_200:
                    recharge_edt_amount.setText(200 + "");
                    break;
                case R.id.recharge_money_500:
                    recharge_edt_amount.setText(500 + "");
                    break;
                case R.id.recharge_money_1000:
                    recharge_edt_amount.setText(1000 + "");
                    break;

            }
            recharge_edt_amount.setSelection(recharge_edt_amount.getText().toString().length());
        }
    };


}
